package com.bym.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bym.mall.consts.MallConst;
import com.bym.mall.entity.User;
import com.bym.mall.form.GetCodeForm;
import com.bym.mall.form.UserLoginForm;
import com.bym.mall.form.UserLoginForm2;
import com.bym.mall.form.UserRegisterForm;
import com.bym.mall.service.IUserService;
import com.bym.mall.vo.ResponseVo;
import com.bym.mall.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bym
 * @since 2022-03-22
 */
@Slf4j
@Api("用户模块")
@RequestMapping("/mall/user")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 注册-用户名+密码
     *
     * @param userRegisterForm 用户注册表单
     * @return {@link ResponseVo}<{@link User}>
     */
    @ApiOperation("注册-用户名+密码")
    @PostMapping("/register")
    public ResponseVo register(@RequestBody @Valid UserRegisterForm userRegisterForm) {
        User user = new User();
        BeanUtil.copyProperties(userRegisterForm, user);
        return userService.register(user);
    }

    /**
     * 登录-用户名+密码
     *
     * @param userLoginForm 用户登录表单
     * @param httpSession   http会话
     * @return {@link ResponseVo}<{@link User}>
     */
    @ApiOperation("登录-用户名+密码")
    @PostMapping("/login")
    public ResponseVo login(@RequestBody @Valid UserLoginForm userLoginForm, HttpSession httpSession) {
        ResponseVo responseVo = userService.loginByPwd(userLoginForm.getUsername(), userLoginForm.getPassword());
        //将用户信息存入session
        httpSession.setAttribute(MallConst.CURRENT_USER, responseVo.getData());
        return responseVo;
    }

    /**
     * 获取验证码
     *
     * @param getCodeForm 获取验证码表单
     * @return {@link ResponseVo}<{@link String}>
     */
    @ApiOperation("获取验证码")
    @PostMapping("/getCode")
    public ResponseVo getCode(@RequestBody @Valid GetCodeForm getCodeForm) {
        return userService.getCode(getCodeForm.getPhone());
    }

    /**
     * 登录：手机号+验证码
     *
     * @param userLoginForm 用户登录表单
     * @return {@link ResponseVo}<{@link User}>
     */
    @ApiOperation("登录：手机号+验证码")
    @PostMapping("/loginByPhoneAndCode")
    public ResponseVo loginByPhoneAndCode(@RequestBody @Valid UserLoginForm2 userLoginForm, HttpSession httpSession) {
        ResponseVo responseVo = userService.loginByPhoneAndCode(userLoginForm.getPhone(), userLoginForm.getCode());
        //将用户信息存入session
        httpSession.setAttribute(MallConst.CURRENT_USER, responseVo.getData());
        return responseVo;
    }

    /**
     * 获取用户信息
     *
     * @param httpSession http会话
     * @return {@link ResponseVo}<{@link User}>
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/userInfo")
    public ResponseVo userInfo(HttpSession httpSession) {
        UserVo userVo = (UserVo) httpSession.getAttribute(MallConst.CURRENT_USER);
        return ResponseVo.success(userVo);
    }

    /**
     * 注销
     *
     * @param httpSession http会话
     * @return {@link ResponseVo}<{@link User}>
     */
    @ApiOperation("注销")
    @GetMapping("/logout")
    public ResponseVo logout(HttpSession httpSession) {
        httpSession.removeAttribute(MallConst.CURRENT_USER);
        return userService.logout();
    }
}
